package database_interactions;


import java.util.ArrayList;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.http.HTTPRepository;

/**
 * @author Christophe Bordier
 */
public class SparqlEndpoint {
	String prefix="prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
			"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
			"prefix xsd: <http://www.w3.org/2001/XMLSchema#> "+
			"prefix d2rq: <http://www.wiwiss.fu-berlin.de/suhl/bizer/D2RQ/0.1#> "+
			"prefix dc: <http://purl.org/dc/elements/1.1/> "+
			"prefix bnf-onto: <http://data.bnf.fr/ontology/bnf-onto/> "+
			"prefix bnfroles: <http://data.bnf.fr/vocabulary/roles/>"+
			"prefix dcterms: <http://purl.org/dc/terms/> "+
			"prefix foaf: <http://xmlns.com/foaf/0.1/> "+
			"prefix skos: <http://www.w3.org/2004/02/skos/core#> "+
			"prefix iswc: <http://annotation.semanticweb.org/iswc/iswc.daml#> "+
			"prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> "+
			"prefix jdbc: <http://d2rq.org/terms/jdbc/> ";
	
	
	public SparqlEndpoint() {
		/** Initialisation des prefixes utiliser pour les predicats des triplets RDF  */ 

	}
	/** Cette methode retourne une liste de triplet  */
	public ArrayList<String> fillListWithResults(ArrayList<String> resultListToFill) throws RepositoryException, MalformedQueryException, QueryEvaluationException {
		
		/** Connection au Sparql endpoint */
		Repository linkData = new HTTPRepository("http://triplestore.irht.cnrs.fr/openrdf-workbench/repositories/irhtFinalV2/query");
		linkData.initialize(); // initialisation de la connection

		/** Requete Sparql Simple qui retourne 10 triplets RDF  */
		String requete1= prefix+
				" Select distinct *"
				+ "where{ ?x ?y ?z } limit 10";
		
		TupleQuery selectQuery1 = linkData.getConnection().prepareTupleQuery(QueryLanguage.SPARQL,requete1);  

		// on l'ex�cute 
		TupleQueryResult selectQueryResult1 = selectQuery1.evaluate();  

		// on it�re sur les r�sultats  
		while(selectQueryResult1.hasNext()) {  
			BindingSet aBinding = selectQueryResult1.next();    
			for (String aBindingName : selectQueryResult1.getBindingNames()) {  
				resultListToFill.add(aBinding.getValue(aBindingName).stringValue());
			}  
		} 
		return resultListToFill;
	}
	

}